using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Web.UI;
using System.Web.UI.WebControls;

using DotNetNuke;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Tabs;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using Common;

using System.Security.Cryptography;
using System.Text;
using System.IO;

namespace AdminTools
{
    partial class ViewAdminTools : PortalModuleBase, IActionable
    {
        #region Private Members
        TabController tabController = new TabController();
        TabInfo tabInfo = new TabInfo();
        ModuleController moduleController = new ModuleController();
        ModuleInfo moduleInfo = new ModuleInfo();
        private int loginUserType
        {
            get
            {
                if (Session["loginUserType"] == null) return 0;
                return (int)Session["loginUserType"];
            }
        }
        private int loginUserPortalId
        {
            get
            {
                if (Session["loginUserPortalId"] == null)
                {
                    return -1;
                    //Response.Redirect(appLibs.GetLoginURL(PortalId), true);
                }
                return (int)Session["loginUserPortalId"];
            }
        }
        #endregion

        #region Public Methods

        public bool DisplayAudit()
        {
            bool retValue = false;

            if ((string)Settings["auditinfo"] == "Y")
            {
                retValue = true;
            }

            return retValue;
        }

        #endregion
       
        #region Optional Interfaces

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Registers the module actions required for interfacing with the portal framework
        /// </summary>
        /// <value></value>
        /// <returns></returns>
        /// <remarks></remarks>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        public ModuleActionCollection ModuleActions
        {
            get
            {
                ModuleActionCollection Actions = new ModuleActionCollection();
                Actions.Add(this.GetNextActionID(), Localization.GetString(ModuleActionType.AddContent, this.LocalResourceFile), ModuleActionType.AddContent, "", "", this.EditUrl(), false, SecurityAccessLevel.Edit, true, false);
                return Actions;
            }
        }

        #endregion

        #region Event Handlers

        protected void Page_Load(System.Object sender, System.EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    btnProduct.Enabled = Common.userRoles.IsSysAccount(loginUserType) &&
                                         Common.userRoles.IsProductManager(loginUserType);
                    
                    btnBuyRequest.Enabled = Common.userRoles.IsSysAccount(loginUserType) &&
                                            Common.userRoles.IsBuyRequestManager(loginUserType);

                    btnEnterprise.Enabled = Common.userRoles.IsSysAccount(loginUserType) &&
                                            Common.userRoles.IsVendorManager(loginUserType);

                    btnPotentialCustomer.Enabled = Common.userRoles.IsSysAccount(loginUserType) &&
                                                   Common.userRoles.IsVendorManager(loginUserType); 
                    
                    btnNews.Enabled = Common.userRoles.IsNewsEditor(loginUserType,loginUserPortalId,PortalId);

                    btnAdvert.Enabled = Common.userRoles.IsAdvertEditor(loginUserType, loginUserPortalId, PortalId);

                    btnSmallAdvert.Enabled = Common.userRoles.IsSmallAdEditor(loginUserType);

                    btnRating.Enabled = Common.userRoles.IsRatingEditor(loginUserType);

                    btnAccount.Enabled = Common.userRoles.IsSysAccount(loginUserType) &&
                                         Common.userRoles.IsUserManager(loginUserType);

                    btnEStore.Enabled = Common.userRoles.IsSysAccount(loginUserType) &&
                                        Common.userRoles.IsStoreManager(loginUserType);

                    btnStatistic.Enabled =  Common.userRoles.IsSysAccount(loginUserType) ||
                                            Common.userRoles.IsMarketingEditor(loginUserType);

                    btnContactReport.Enabled = Common.userRoles.IsSysAccount(loginUserType) ||
                                               Common.userRoles.IsReportManager(loginUserType);

                    btnChangeOwnership.Enabled = Common.userRoles.IsSysAccount(loginUserType) ||
                                               Common.userRoles.IsProductManager(loginUserType);
                    
                    btnCategoryTree.Enabled = Common.userRoles.IsSysAccount(loginUserType) ||
                                              Common.userRoles.IsConfigManager(loginUserType);
                    btnConfig.Enabled = Common.userRoles.IsSysAccount(loginUserType) ||
                                        Common.userRoles.IsConfigManager(loginUserType);
                    
                    btnSendMail.Enabled = Common.userRoles.IsSysAccount(loginUserType) ||
                                          Common.userRoles.IsMarketingManager(loginUserType);

                    //The control is visible if there is any  button is visible;
                    this.Parent.Parent.Parent.Parent.Visible =
                       (btnProduct.Enabled || btnBuyRequest.Enabled ||
                        btnEnterprise.Enabled || btnPotentialCustomer.Enabled ||
                        btnNews.Enabled || btnAdvert.Enabled || btnSmallAdvert.Enabled ||
                        btnRating.Enabled || btnAccount.Enabled || btnEStore.Enabled || btnStatistic.Enabled || btnConfig.Enabled ||
                        btnContactReport.Enabled || btnChangeOwnership.Enabled || btnCategoryTree.Enabled ||
                        btnConfig.Enabled || btnSendMail.Enabled);
                }
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }
        protected void btnAccout_Click(object sender, EventArgs e)
        {
            int tabId = appLibs.GetTabIdByName(PortalId,"systemUser");
            Response.Redirect(DotNetNuke.Common.Globals.NavigateURL(tabId));
        }
        protected void btnAdvert_Click(object sender, EventArgs e)
        {
            int tabId = appLibs.GetTabIdByName(PortalId, "advert");
            Response.Redirect(DotNetNuke.Common.Globals.NavigateURL(tabId));
        }
        protected void btnNews_Click(object sender, EventArgs e)
        {
            int tabId = appLibs.GetTabIdByName(PortalId, "newsmana");
            Response.Redirect(DotNetNuke.Common.Globals.NavigateURL(tabId));
        }
        protected void btnSmallAdvert_Click(object sender, EventArgs e)
        {
            int tabId = appLibs.GetTabIdByName(PortalId, "smalladvert");
            Response.Redirect(DotNetNuke.Common.Globals.NavigateURL(tabId));
        }
        protected void btnEnterprise_Click(object sender, EventArgs e)
        {
            tabInfo = tabController.GetTabByName("enterprise", PortalSettings.PortalId);
            moduleInfo = moduleController.GetModuleByDefinition(PortalSettings.PortalId, "enterprise");
            Response.Redirect(DotNetNuke.Common.Globals.NavigateURL(tabInfo.TabID, "search", "mid=" + moduleInfo.ModuleID));
        }
        protected void btnRating_Click(object sender, EventArgs e)
        {
            int tabId = appLibs.GetTabIdByName(PortalId, "rating");
            Response.Redirect(DotNetNuke.Common.Globals.NavigateURL(tabId));
        }
        protected void btnProduct_Click(object sender, EventArgs e)
        {
            tabInfo = tabController.GetTabByName("product", PortalSettings.PortalId);
            moduleInfo = moduleController.GetModuleByDefinition(PortalSettings.PortalId, "product");
            Response.Redirect(DotNetNuke.Common.Globals.NavigateURL(tabInfo.TabID, "search", "mid=" + moduleInfo.ModuleID));
        }
        protected void btnBuyRequest_Click(object sender, EventArgs e)
        {
            tabInfo = tabController.GetTabByName("buyRequest", PortalSettings.PortalId);
            moduleInfo = moduleController.GetModuleByDefinition(PortalSettings.PortalId, "buyRequest");
            Response.Redirect(DotNetNuke.Common.Globals.NavigateURL(tabInfo.TabID, "search", "mid=" + moduleInfo.ModuleID));
        }
        protected void btnCategoryTree_Click(object sender, EventArgs e)
        {
            int tabId = appLibs.GetTabIdByName(PortalId, "manageTree");
            Response.Redirect(DotNetNuke.Common.Globals.NavigateURL(tabId));
        }
        protected void btnEStore_Click(object sender, EventArgs e)
        {
            int tabId = Common.appLibs.GetTabIdByName(PortalId, "store");
            int moduleId = Common.appLibs.GetModuleIdByName(PortalId, "store");
            Response.Redirect(DotNetNuke.Common.Globals.NavigateURL(tabId, "search", "mid=" + moduleId));
        }
        protected void btnContactReport_Click(object sender, EventArgs e)
        {
            int tabId = appLibs.GetTabIdByName(PortalId, "contactReport");
            Response.Redirect(DotNetNuke.Common.Globals.NavigateURL(tabId));
        }
        protected void btnConfig_Click(object sender, EventArgs e)
        {
            int tabId = appLibs.GetTabIdByName(PortalSettings.PortalId, "sysOptions");
            Response.Redirect(DotNetNuke.Common.Globals.NavigateURL(tabId));
        }
        protected void btnStatistic_Click(object sender, EventArgs e)
        {
            int tabId = appLibs.GetTabIdByName(PortalId, "statistic");
            Response.Redirect(DotNetNuke.Common.Globals.NavigateURL(tabId));
        }
        protected void btnChangeOwnership_Click(object sender, EventArgs e)
        {
            int tabId = appLibs.GetTabIdByName(PortalSettings.PortalId, "ChangeOwnership");
            Response.Redirect(DotNetNuke.Common.Globals.NavigateURL(tabId));
        }
        protected void btnPotentialCustomer_Click(object sender, EventArgs e)
        {
            int tabId = appLibs.GetTabIdByName(PortalId, "potentialCustomer");
            Response.Redirect(DotNetNuke.Common.Globals.NavigateURL(tabId));
        }
        protected void btnSendMail_Click(object sender, EventArgs e)
        {
            int tabId = appLibs.GetTabIdByName(PortalId, "sendmail");
            Response.Redirect(DotNetNuke.Common.Globals.NavigateURL(tabId));
        }
        protected void btnImport_Click(object sender, EventArgs e)
        {
            int tabId = appLibs.GetTabIdByName(PortalId, "migrate");
            Response.Redirect(DotNetNuke.Common.Globals.NavigateURL(tabId));
        }
        #endregion
    }
}

